/****************************************************************************************************************************
 * @File name:power.c
 * @Author:sunshu
 * @Version:v1.00
 * @Date:2018/01/20
 * @Description:
 * @Others:
 * @Function List:
 * @History:
****************************************************************************************************************************/

#include "stm32f4xx_hal.h"
#include "main.h"
#include "delay.h"
#include "power.h"
#include "led.h"
#include "e_ink.h"
#include "usart.h"


#define POWER_HIGH() 			HAL_GPIO_WritePin(POWER_EN_GPIO_Port,POWER_EN_Pin,GPIO_PIN_SET)
#define POWER_LOW()				HAL_GPIO_WritePin(POWER_EN_GPIO_Port,POWER_EN_Pin,GPIO_PIN_RESET)

unsigned long UsbPowerCount = 0;
unsigned char PowerState = 0;
unsigned char PowerFlag = 0;

/****************************************************************************************************************************
* @Functions:PowerDet()
* @Description:the mcu system is sleept when no doings during 1 minute.
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void PowerDet(void)
{
    if(HAL_GPIO_ReadPin(BQ24073_NPGOOD_GPIO_Port, BQ24073_NPGOOD_Pin))
    {
        UsbPowerCount++;
        if(UsbPowerCount >= AUTO_POWEROFF*100)			//10ms*100*60 = 60s
        {
            PowerState = POWERMODE_OFF;
            PowerFlag = 1;
        }
    }
    else
    {
        UsbPowerCount = 0;
    }
}

/****************************************************************************************************************************
* @Functions:PowerOn()
* @Description:enable the power pin on power
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void PowerOn(void)
{
    POWER_HIGH();
}

/****************************************************************************************************************************
* @Functions:PowerOff()
* @Description:disable the power pin when power off
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void PowerOff(void)
{
    if(HAL_GPIO_ReadPin(BQ24073_NPGOOD_GPIO_Port, BQ24073_NPGOOD_Pin))			//
    {
        POWER_LOW();
    }
    LedTask(TEST_OFF);
}

/****************************************************************************************************************************
* @Functions:PowerProcess()
* @Description:deal power pin
* @Input:
* @Output:
* @Return:
* @Others:
* @History:
****************************************************************************************************************************/
void PowerProcess(void)
{
    if(PowerFlag)
    {
        PowerFlag = 0;
        switch(PowerState)
        {
        case POWERMODE_ON:
            PowerOn();
            break;

        case POWERMODE_OFF:
            delay_ms(300);
            PowerOff();
            delay_ms(1000);
            break;

        case POWERMODE_IDLE:
            LedTask(TEST_OFF);
            break;
        }
    }
}

